//
//  WBVisitorView.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/3.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
import SnapKit

//定义一个协议 继承自NSOBjectProtol
//---------------------------------------------------
protocol WBVisitorViewDelegate:NSObjectProtocol {
    func didshowRegister() //注册方法
    func didshowLogin()    //登录方法

}
//====================================================

class WBVisitorView: UIView {
    /// -----------------方法二:第一步定义闭包----------------
    //定义注册闭包
    var resisterClose: (()->())?
    //定义登录闭包属性
    var resisLogin: (()->())?
   //------------------------------------------------------
    //===========定义代理
    weak var delegate: WBVisitorViewDelegate?
    
    
    /**
     自定义视图三步骤
     
     第一步: 重写父类的构造方法
     第二步: 懒加载
     第三步: 添加子控件和设置约束
     */
     
     //MARK: - 第一步:重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加约束
      setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: -- 第三步 设置约束和添加子控件
    private func setupUI() {
        //设置透明度来调试View颜色
        backgroundColor = UIColor(white: 234.0/255.0, alpha: 1)
        addSubview(turnImageView)//转动视图
        addSubview(maskImageView) //遮盖视图
        
        addSubview(bgImageView)  //中间房子
        addSubview(notiLable)    //简介说明
        addSubview(registerButton) //注册
        addSubview(loginButton)    //登录
        
     
        
        //设置背景约束
        bgImageView.snp_remakeConstraints { (make) -> Void in
            //设置中心与父控件view对齐
            make.center.equalTo(self.snp_center)
        }
        
        //旋转视图使用另外一种方法
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        //遮盖视图
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //水平居中,垂直居中 上,下,左,右,宽,高,一个控件不超过4个约束
        notiLable.snp_makeConstraints { (make) -> Void in
         make.centerX.equalTo(self.snp_centerX)
         make.top.equalTo(turnImageView.snp_bottom).offset(10)
         make.width.equalTo(216)
        }
        
        //注册按钮 左边与显示的notiLable左边对齐 上边与notiLable的底部 宽和高固定
        registerButton.snp_makeConstraints { (make) -> Void in
          make.left.equalTo(notiLable.snp_left)
          make.top.equalTo(notiLable.snp_bottom).offset(10)
          make.width.equalTo(100)
          make.height.equalTo(30)
            
        }
        
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(notiLable.snp_right)
            make.top.equalTo(notiLable.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
    
    }
    
    

    //MARK: -- 第二步懒加载
    //遮盖转动的视图
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return img
    }()
    
    
    
    //首页小房子背景
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        return img
    }()
    
    /**
     *  首页转动图片
     */
    
    private lazy var turnImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return img
    }()
    
    
    //提示laber
    private lazy var notiLable: UILabel = {
        let noti = UILabel()
        //字体中间显示
        noti.textAlignment = .Center
        //字体尺寸
        noti.font = UIFont.systemFontOfSize(15)
        //设置颜色
        noti.textColor = UIColor.darkGrayColor()
        noti.text = "关注一些人,回这里看看有什么惊喜"
        noti.numberOfLines = 0
        return noti
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //点击方法
        button.addTarget(self, action: "showRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //点击方法
        button.addTarget(self, action: "showLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    
    
    //提供一个接口 在方法中 修改私有属性
    func setupInfo(title:String,image:String?) {
        if let image = image {  //非首页
            notiLable.text = title
            bgImageView.image = UIImage(named: image)
            turnImageView.hidden = true
        }else { //首页
         turnImageView.hidden = false
            //开始首页动画
            startAnimation()
        }
    }
    
    
    //注册按钮点击方法  在按钮的点击方法中 一般用private 修饰为私有方法 ,这样在程序运行着 
    //触发事件会导致找不到这个方法  需要在private前面 @objc 因为swift是运行时机制
 @objc private  func showRegister() {
        
        //print("点击了注册");
        ///调用注册闭包
       //resisterClose?()
        //使用代理
    delegate?.didshowRegister()
    
    }
    
    
    
    //登录
 @objc private  func showLogin() {
        //print("点击了登录")
        ///第二步调用登录闭包
       //resisLogin?()
    
       //第二种方法调用代理
        delegate?.didshowLogin()
    }
    
    
    //MARK: -- 首页转动动画
    func startAnimation() {
        //旋转类型
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 10
        animation.toValue = 2 * M_2_PI
        animation.repeatCount = MAXFLOAT
       //切换界面的时候 动画释放 指定为false 就不会自动释放
        animation.removedOnCompletion = false
       
        turnImageView.layer.addAnimation(animation, forKey: nil)
    
    }
    
    
    
}
